<template>
  <div class="app-container">
    <div class="search-container">
      <el-form ref="queryFormRef" :model="queryParams" :inline="true">
        <el-form-item label="开始时间" prop="startTime">
          <el-date-picker
            v-model="queryParams.startTime"
            type="date"
            placeholder="请选择"
            :disabled-date="disabledDate"
            @change="changeStartDay"
          />
        </el-form-item>
        <el-form-item label="结束时间" prop="endTime">
          <el-date-picker
            v-model="queryParams.endTime"
            type="date"
            :disabled="!queryParams.startTime"
            placeholder="请选择"
            :disabled-date="disabledEndDate"
            @change="changeEndDay"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleQuery"><i-ep-search />搜索</el-button>
          <el-button @click="resetQuery">
            <i-ep-refresh />
            重置</el-button
          >
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never">
      <el-form-item>
        <el-text type="danger" style="margin-left: 20px">统计截止时间为昨天</el-text>
      </el-form-item>
      <el-table v-loading="loading" :data="tableList" highlight-current-row>
        <el-table-column label="序号" type="index" min-width="60" align="center"></el-table-column>
        <el-table-column key="date" label="考勤日期" min-width="80" align="center" prop="date" />
        <el-table-column
          key="numberAttendees"
          label="应考勤人数"
          align="center"
          min-width="300"
          prop="numberAttendees"
        />
        <el-table-column
          key="actualAttendance"
          label="实际考勤人数"
          align="center"
          min-width="100"
          prop="actualAttendance"
        />
        <el-table-column
          key="abnormalNumberPeople"
          label="异常人数"
          align="center"
          min-width="100"
          prop="abnormalNumberPeople"
        />
      </el-table>

      <pagination
        v-if="total > 0"
        v-model:total="total"
        v-model:page="queryParams.pageIndex"
        v-model:limit="queryParams.pageSize"
        @pagination="handleQuery"
      />
    </el-card>
  </div>
</template>

<script lang="ts" setup>
import { ref, reactive, computed } from "vue";
import moment from "moment";
import { QueryPageProjectDay } from "@/api/checkin/index";
import { useScreen } from "@/store/modules/screen";

const props = defineProps({
  id: {
    type: Number,
    default: 0,
  },
});
const { id } = toRefs(props);

const total = ref(0);
const loading = ref(false);
const queryFormRef = ref(ElForm);
const queryParams = reactive<any>({
  pageIndex: 1,
  pageSize: 20,
  projectId: 0,
});
const tableList = ref<any[]>([]);

/**
 * 禁用日期
 */
const disabledDate = (time: Date) => {
  return time.getTime() > moment().toDate().getTime();
};
const disabledEndDate = (time: Date) => {
  if (queryParams.startTime) {
    return (
      time.getTime() < moment(queryParams.startTime).toDate().getTime() || time.getTime() > moment().toDate().getTime()
    );
  }
  return time.getTime() > moment().toDate().getTime();
};

/* 列表查询 */
async function handleQuery() {
  loading.value = true;
  queryParams.projectId = id.value;
  let list = await QueryPageProjectDay(queryParams);
  if (list.type === 0) {
    if (list.success) {
      tableList.value = list.data.list;
      queryParams.pageIndex = list.data.pageNum;
      queryParams.pageSize = list.data.pageSize;
      total.value = list.data.total;
    } else {
      ElMessage.error(list.msg);
    }
  } else {
    ElMessage.error(list.msg);
  }
  loading.value = false;
}

function changeStartDay(val: string) {
  if (val === null) queryParams.startTime = undefined;
  else queryParams.startTime = moment(val).format("YYYY-MM-DD");
}
function changeEndDay(val: string) {
  if (val === null) queryParams.endTime = undefined;
  else queryParams.endTime = moment(val).format("YYYY-MM-DD");
}
/**
 * 重置查询
 */
function resetQuery() {
  queryParams.startTime = undefined;
  queryParams.endTime = undefined;
  queryParams.pageIndex = 1;
  queryParams.pageSize = 20;
  handleQuery();
}

onMounted(() => {
  handleQuery();
});
</script>

<style lang="less" scoped></style>
